var $ = require('jquery');
var chatPeer = require('../../chatPeer');
var html = require('./index.html');
var api = require('../../api');
var util = require('../../../utils');
var $inputAreaTmp = $(html);
module.exports.create = function ($parent) {
  var $inputArea = $inputAreaTmp.clone();
  var $input = $inputArea.find('.ipt-content');
  var input = $input[0];

  input.addEventListener && input.addEventListener('paste', function (event) {
    var clipboardData = event.clipboardData || window.clipboardData;
    if (clipboardData) {
      var items = clipboardData.items;
      if (!items) return;

      // 提取图片或文本
      var item = items[0];
      var types = clipboardData.types || [];
      for (var i = 0; i < types.length; i++) {
        if (types[i] === 'Files') {
          item = items[i];
          break;
        }
      }
      // 图片
      if (item && item.kind === 'file' && item.type.match(/^image\//i)) {
        var file = item.getAsFile();
        uploadAndInertImage(file).then(function () {
          var sel = window.getSelection();
          var range = document.createRange();
          range.selectNodeContents(input);
          range.collapse(false);
          sel.removeAllRanges();
          sel.addRange(range);
        });
      }
    }

    if (item && item.kind === 'string') {
      item.getAsString(function (str) {
        $input.append(str);
        var sel = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(input);
        range.collapse(false);
        sel.removeAllRanges();
        sel.addRange(range);
      });
    }

    event.preventDefault();
  });
  $parent.append($inputArea);

  $input.on('keyup', function () {
    var len = $(this).text().length;
    var $tips = $(this).closest('.im-edit-area').find('.im-edit-tip');
    //if (len <= 350) {
    //  $tips.html('还可以输入<span class="still-count">' + (350 - len) + '</span>字');
    //  return;
    //}
    //$tips.html('您输入的文字已经超过<span class="still-count">' + (len - 350) + '</span>字');
    $tips.html('您已输入<span class="still-count">' + len+ '</span>个字');
  });

  function uploadAndInertImage(file) {
    var ext = file.type.split('/')[1];
    var filename = util.createRandomId() + '.' + ext;
    var url = '';
    return api.getOssConfig().then(function (conf) {
      var filepath = conf.dir + filename;
      var formData = new FormData();
      formData.append('key', filepath);
      formData.append('policy', conf.policy);
      formData.append('OSSAccessKeyId', conf.accessKeyId);
      formData.append('success_action_status', '200');
      formData.append('signature', conf.signature);
      formData.append('file', file);
      url = conf.url + '/' + filepath;
      return $.ajax({
        url: conf.url,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST'
      });
    }).then(function () {
      console.log('上传图片成功', url);
      var $img = $('<img>');
      $img.css({
        'max-width': '120px',
        height: 'auto'
      });
      $img.attr('src', url);
      $img.attr('contenteditable', false);
      $img.attr('oncontrolselect', 'return false;');
      $input.append($img);
    });
  }

};

